/*-----------------------------------------------------------------------------------*/
/*                                                                                   */
/* Copyright (c) 2008 Nuvoton Technology Corporation                                 */
/* All rights reserved                                                               */
/*                                                                                   */
/*-----------------------------------------------------------------------------------*/
/* File Contents:                                                                    */
/*   ps2_barcode.c                                                                   */
/*                                                                                   */
/* This file contains:                                                               */
/*                                                                                   */
/* Project:                                                                          */
/*                                                                                   */
/* Remark:                                                                           */
/*   1. The last data byte of bar-code is 0x0D.                                      */
/*   2. In the front of each lower case characters, there is an additional byte      */
/*      0x2E.                                                                        */
/*                                                                                   */
/*-----------------------------------------------------------------------------------*/

#ifdef ECOS 
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "kapi.h"
#include "diag.h"
#include "wbio.h"
#else
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "wblib.h"
#endif

#include "NUC900_PS2.h"


#ifdef ECOS
#define ps2printf		diag_printf
#define ps2getchar		diag_getc
#else
#define ps2printf		sysprintf
#define ps2getchar		sysGetChar
#endif 

#ifdef ECOS
static cyg_handle_t  	thread_handle;
static cyg_thread 		thread;
#define STACKSIZE		(128*1024)
static UINT8            _Statck[STACKSIZE];
#endif

static PS2_DATA_T rxb[20];

//------------------------- Program -------------------------//
int WB_GetNum ()
{
	int i = 0;
	char val = 0, temp[6];  

	do
	{
		val = ps2getchar();
		if(val != 'q')
			temp[i++] = val;

	}while(val != 0x0D);  // Enter key

	temp[i] = '\0';	
	i = atoi(temp);
	
	return i;
}	

#ifdef ECOS
void _ps2BarcodeExample (cyg_addrword_t data)
#else
void _ps2BarcodeExample ()
#endif
{
	int retval, num, ch, port, i = 0;	
		
	ps2printf("\n\nBar-code Example ...\n");
	
	while (1)
	{
		ps2printf("\n Select PS2 channel [0/1]\n");
		
		ch = ps2getchar();
		if ( (ch != '0') && (ch != '1') )
			continue;
		else
		{
			if (ch == '0')
				port = PS2_PORT0;
			else
				port = PS2_PORT1;	
				
			break;	
		}	
	}
	ps2printf(" Channel[%d] selected\n", port);		
	
	ps2Init();
	retval = ps2Open(port, PS2_BARCODE);
	if (retval != 0)
	{
		ps2printf("\n Open PS2 device fail!\n");	
		return;
	}			
	
	while (1)
	{
		ps2printf("\n Set Rx operation mode block/non-block [0/1]\n");
		
		ch = ps2getchar();
		if ( (ch == '0') || (ch == '1') )
			break;	
	}		
			
	if (ch == '0')
	{
		/*
			Block mode:
			The bar-code number should be equal to the parameter "num".                                                              
			The "num" value can't count the last byte of bar-code, 0x0D.
		*/
		ps2printf(" Block mode selected\n");	
		retval = ps2Ioctl(port, PS2_IOC_SET_RX_MODE, PS2_BLOCK, 0);
		if (retval != 0)
			ps2printf("Set block mode failed!\n");
		else
		{
			ps2printf("\n keyin bar-code number [6~13], then press Enter\n");
			num = WB_GetNum();
			ps2printf(" number[%d]\n", num);		
			ps2printf("\nStart to test\n");	
			
			while (1)
			{
				retval = ps2Read(port, (PUINT8)&rxb, num + 1);
				if (retval > num)
				{
					for (i = 0; i < retval; i++)
						ps2printf("%c", rxb[i].ucAsciiCode);
					ps2printf("\n");
				}
			}
		}
	}
	else
	{
		/*
			Non-Block mode:
			For bar-code application, non-block mode is recommended.			                                                              
		*/	
		ps2printf(" Non-block mode selected\n");	
		retval = ps2Ioctl(port, PS2_IOC_SET_RX_MODE, PS2_NON_BLOCK, 0);
		if (retval != 0)
			ps2printf("Set block mode failed!\n");
		else
		{
			ps2printf("\nStart to test\n");				
			while (1)
			{
				retval = ps2Read(port, (PUINT8)&rxb, 1);	
				if (retval > 0)
				{
					if (rxb[0].ucAsciiCode != 0x0D)
					{  
						if (rxb[0].ucAsciiCode != 0x2E) 
						    ps2printf("%c", rxb[0].ucAsciiCode);
					}
					else
						ps2printf("\n");		
				}
			}
		}		
	}	
}	

int main ()
{


#ifdef ECOS

	cyg_thread_create(20, _ps2BarcodeExample, 0, "ps2barcode", _Statck, STACKSIZE, &thread_handle, &thread);
	cyg_thread_resume(thread_handle);

#else
	_ps2BarcodeExample();
#endif	
	
	return 0;	
}
